home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / IGEMatrix.h < prev    next >
C/C++ Source or Header  |  1989-08-18  |  5KB  |  164 lines

  1. #ifndef IGEMATRIX_H
  2. #define IGEMATRIX_H
  3. #pragma once 
  4.  
  5. /*
  6.  *    Declarations for int general matricies.
  7.  *
  8.  *    Copyright (C) 1988, 1989.
  9.  *
  10.  *    Dr. Thomas Keffer
  11.  *    Rogue Wave Associates
  12.  *    P.O. Box 85341
  13.  *    Seattle WA 98145-1341
  14.  *
  15.  *    Permission to use, copy, modify, and distribute this
  16.  *    software and its documentation for any purpose and
  17.  *    without fee is hereby granted, provided that the
  18.  *    above copyright notice appear in all copies and that
  19.  *    both that copyright notice and this permission notice
  20.  *    appear in supporting documentation.
  21.  *    
  22.  *    This software is provided "as is" without any
  23.  *    expressed or implied warranty.
  24.  *
  25.  *
  26.  *    @(#)IGEMatrix.h    2.1    8/18/89
  27.  */
  28.  
  29. /*
  30.  *    This class is derived from class IntVec.  Data is stored
  31.  *    FORTRAN style: by columns.
  32.  *
  33.  *    Defining the preprocessor directive "BOUNDS_CHECK" will invoke
  34.  *    bounds checking.
  35.  */
  36.  
  37. #include "IntVec.h"
  38.  
  39. class IGEMatrix : public IntVec {
  40.   int ncols;            // Number of columns
  41.   int nrows;            // Number of rows
  42. protected:
  43.   void             assertColRange(int);
  44.   void             assertRowRange(int);
  45.   void            assertRowCol(const IGEMatrix&);
  46.   void            assertLength(const IntVec&);
  47.   void            assertSquare();
  48.   void            assertProduct(const IGEMatrix&);
  49.   void            assertProduct(const IntVec&);
  50. public:
  51.   IGEMatrix();
  52.   IGEMatrix(int rows, int cols);
  53.   IGEMatrix(int rows, int cols, int initval);
  54.   IGEMatrix(const int* dat, int, int);  // Copy of dat will be made
  55.   IGEMatrix(const IntVec& v, int, int); // Reference to v will be made
  56.   IGEMatrix(const IGEMatrix& m);       // Reference to m will be made
  57.  
  58.   int*            data()        {return IntVec::data();}
  59.   int            cols();
  60.   int            rows();
  61.  
  62.   IGEMatrix&        reference(IGEMatrix& m); // Reference self to m
  63.   IGEMatrix        deepCopy();    // copy of self with distinct instance variables 
  64.   IGEMatrix        copy()        {return deepCopy();} // Synonym for deepCopy()
  65.   void            deepenShallowCopy();    // Guarantee that references==1:
  66.  
  67.   IntVec        operator[](int j);    // Return a col as a slice
  68.   IntVec        col(int j);        // Return a col as a slice
  69.   IntVec        row(int i);        // Return a row as a slice
  70.   IntVec        diagonal(int idiag=0);    // Return a diagonal as a slice
  71.   int&            operator()(int i, int j); // Subscripting
  72.  
  73. // Math functions
  74.   IGEMatrix        product(const IGEMatrix&); // Inner product
  75.   IntVec        product(const IntVec&);
  76.  
  77. // Assignment operators --- self must be same size as m
  78.   IGEMatrix&        operator=(const IGEMatrix& m);
  79.   IGEMatrix&        operator=(int);
  80.   IGEMatrix&        operator+=(const IGEMatrix& m);
  81.   IGEMatrix&        operator+=(int);
  82.   IGEMatrix&        operator-=(const IGEMatrix& m);
  83.   IGEMatrix&        operator-=(int);
  84.   IGEMatrix&        operator*=(const IGEMatrix& m);
  85.   IGEMatrix&        operator*=(int);
  86. //IGEMatrix&        operator/=(const IGEMatrix& m);
  87. //IGEMatrix&        operator/=(int);
  88.  
  89. // Increment/decrement operators
  90.   IGEMatrix&        operator++();
  91.   IGEMatrix&        operator--();
  92.  
  93. // Friendly arithmetic operators; Notice that operator* is an element-by-
  94. // element multiply, NOT a matrix multiply.
  95.   friend IGEMatrix    operator-(const IGEMatrix&);    // Unary minus
  96.   friend IGEMatrix    operator+(const IGEMatrix&);    // Unary plus
  97.   friend IGEMatrix    operator*(const IGEMatrix&, const IGEMatrix&);
  98. //friend IGEMatrix    operator/(const IGEMatrix&, const IGEMatrix&);
  99.   friend IGEMatrix    operator+(const IGEMatrix&, const IGEMatrix&);
  100.   friend IGEMatrix    operator-(const IGEMatrix&, const IGEMatrix&);
  101.   friend IGEMatrix    operator*(const IGEMatrix&, int);
  102.   friend IGEMatrix    operator*(int, const IGEMatrix&);
  103. //friend IGEMatrix    operator/(const IGEMatrix&, int);
  104. //friend IGEMatrix    operator/(int, const IGEMatrix&);
  105.   friend IGEMatrix    operator+(const IGEMatrix&, int);
  106.   friend IGEMatrix    operator+(int, const IGEMatrix&);
  107.   friend IGEMatrix    operator-(const IGEMatrix&, int);
  108.   friend IGEMatrix    operator-(int, const IGEMatrix&);
  109.  
  110. };
  111.  
  112. // Other (related) declarations:
  113. ostream&        operator<<(ostream& s, const IGEMatrix& m);
  114. IGEMatrix        transpose(const IGEMatrix&);
  115.  
  116. /******************* I N L I N E S **************************/
  117.  
  118. Inline int IGEMatrix::cols() { return ncols;}
  119. Inline int IGEMatrix::rows() { return nrows;}
  120. Inline void IGEMatrix::deepenShallowCopy(){IntVec::deepenShallowCopy();}
  121. Inline IGEMatrix operator+(const IGEMatrix& m)        { return m; }
  122. Inline IGEMatrix operator*(int d, const IGEMatrix& m){ return m*d; }
  123. Inline IGEMatrix operator+(int d, const IGEMatrix& m){ return m+d; }
  124.  
  125. // Return a column
  126. Inline IntVec IGEMatrix::operator[](int j){
  127. #if BOUNDS_CHECK
  128.   assertColRange(j);
  129. #endif
  130.   return IntVec::slice(j*nrows,nrows,1);
  131. }
  132.  
  133. Inline IntVec IGEMatrix::col(int j){    // Same as above
  134. #if BOUNDS_CHECK
  135.   assertColRange(j);
  136. #endif
  137.   return IntVec::slice(j*nrows,nrows,1);
  138. }
  139.  
  140. Inline IntVec IGEMatrix::row(int i){
  141. #if BOUNDS_CHECK
  142.   assertRowRange(i);
  143. #endif
  144.   return IntVec::slice(i, ncols, nrows);
  145. }
  146.  
  147. Inline IntVec IGEMatrix::diagonal(int i){
  148.   register int iabs=abs(i);
  149. #if BOUNDS_CHECK
  150.   assertSquare();
  151.   assertRowRange(iabs);
  152. #endif
  153.   return IntVec::slice(i>0 ? i*nrows : iabs, nrows-iabs, nrows+1);
  154. }
  155.  
  156. Inline int& IGEMatrix::operator()(int i, int j){
  157. #if BOUNDS_CHECK
  158.   assertRowRange(i); assertColRange(j);
  159. #endif
  160.   return IntVec::operator()(j*nrows+i);
  161. }
  162.  
  163. #endif IGEMATRIX_HXX
  164.